<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db as portraitDB;

class Collection extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 期刊论文折线图
     */
    public function techChartIndex()
    {
        $years = model('collection')->field('year, count(year) as count')->whereIn('resourceCode', 'J,I,E,U')
            ->group('year')->order('year')->select()->toArray();
        $obj['years'] = $years;
        return $this->result($obj, 1);
    }

    /**
     * 期刊论文列表
     */
    public function techIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['patentType'])) {
            $where['patentType'] = $param['patentType'];
        }
        if (isset($param['yearFrom']) && isset($param['yearTo'])) {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom'])) {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo'])) {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege'])) {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['level'])) {
            $where['level'] = $param['level'];
        }
        if (isset($param['creator'])) {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        $list = model('collection')
            ->field("id,title,creator,projectNo,journal,volume,url,applicationDate,publicationDate")
            ->whereIn('resourceCode', 'J,I,E,U')
            ->where($where)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('techIndex', ['list' => $list]);
    }

    /**
     * 期刊论文导出
     */
    public function techExport()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['patentType']) && $param['patentType'] != '') {
            $where['patentType'] = $param['patentType'];
        }
        if (isset($param['yearFrom']) && $param['yearFrom'] != '' && isset($param['yearTo']) && $param['yearTo'] != '') {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom']) && $param['yearFrom'] != '') {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo']) && $param['yearTo'] != '') {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege']) && $param['secondaryCollege'] != '') {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['level']) && $param['level'] != '') {
            $where['level'] = $param['level'];
        }
        if (isset($param['creator']) && $param['creator'] != '') {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        $data = model('collection')
            ->field("projectNo,title,creator,journal,volume,url,publicationDate")
            ->whereIn('resourceCode', 'J,I,E,U')
            ->where($where)
            ->select()
            ->toArray();
        array_unshift($data, ['编号', '标题', '作者', '期刊', '年卷期', '原文地址', '入库时间']);
        insert_admin_log('导出了期刊论文');
        $res = export_excel_ajax($data, date('YmdHis'));
        return $this->result($res, 1);
    }


    /**
     * 知识产权折线图
     */
    public function ipChartIndex()
    {
        $years = model('collection')->field('year, count(year) as count')->where('resourceCode', '=', 'P')
            ->group('year')->order('year')->select()->toArray();
        $obj['years'] = $years;
        return $this->result($obj, 1);
    }

    /**
     * 知识产权列表
     */
    public function ipIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['patentType'])) {
            $where['patentType'] = $param['patentType'];
        }
        if (isset($param['yearFrom']) && isset($param['yearTo'])) {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom'])) {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo'])) {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege'])) {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator'])) {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        $list = model('collection')
            ->field("projectNo,title,projectNo,patentType,applicationDate,publicationDate")
            ->where('resourceCode', '=', 'P')
            ->where($where)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('ipIndex', ['list' => $list]);
    }

    /**
     * 知识产权导出
     */
    public function ipExport()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['patentType']) && $param['patentType'] != '') {
            $where['patentType'] = $param['patentType'];
        }
        if (isset($param['yearFrom']) && $param['yearFrom'] != '' && isset($param['yearTo']) && $param['yearTo'] != '') {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom']) && $param['yearFrom'] != '') {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo']) && $param['yearTo'] != '') {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege']) && $param['secondaryCollege'] != '') {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator']) && $param['creator'] != '') {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        $data = model('collection')
            ->field("projectNo,title,projectNo,patentType,applicationDate,publicationDate")
            ->where('resourceCode', '=', 'P')
            ->where($where)
            ->select()
            ->toArray();
        array_unshift($data, ['编号/财产号', '标题', '产权所有人', '产权类型', '申请时间', '公开时间']);
        insert_admin_log('导出了知识产权');
        $res = export_excel_ajax($data, date('YmdHis'));
        return $this->result($res, 1);
    }

    /**
     * 成果折线图
     */
    public function resultChartIndex()
    {
        $years = model('collection')->field('year, count(year) as count')->where('resourceCode', '=', 'A')
            ->group('year')->order('year')->select()->toArray();
        $obj['years'] = $years;
        return $this->result($obj, 1);
    }

    /**
     * 成果列表
     */
    public function resultIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['yearFrom']) && isset($param['yearTo'])) {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom'])) {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo'])) {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege'])) {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator'])) {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        if (isset($param['level'])) {
            $where['level'] = $param['level'];
        }
        $list = model('collection')
            ->field("title,creator,projectNo,patentType,level,publicationDate")
            ->where('resourceCode', '=', 'A')
            ->where($where)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('resultIndex', ['list' => $list]);
    }

    /**
     * 成果导出
     */
    public function resultExport()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['yearFrom']) && $param['yearFrom'] != '' && isset($param['yearTo']) && $param['yearTo'] != '') {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom']) && $param['yearFrom'] != '') {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo']) && $param['yearTo'] != '') {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege']) && $param['secondaryCollege'] != '') {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator']) && $param['creator'] != '') {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        if (isset($param['level']) && $param['level'] != '') {
            $where['level'] = $param['level'];
        }
        $data = model('collection')
            ->field("projectNo,title,creator,patentType,level,publicationDate")
            ->where('resourceCode', '=', 'A')
            ->where($where)
            ->select()
            ->toArray();
        array_unshift($data, ['编号', '成果名称', '获奖人/团队', '成果类型', '获奖级别', '公开时间']);
        insert_admin_log('导出了成果');
        $res = export_excel_ajax($data, date('YmdHis'));
        return $this->result($res, 1);
    }

    /**
     * 科研项目折线图
     */
    public function scientificChartIndex()
    {
        $years = model('collection')->field('year, count(year) as count')->where('resourceCode', '=', 'O')
            ->group('year')->order('year')->select()->toArray();
        $obj['years'] = $years;
        return $this->result($obj, 1);
    }

    /**
     * 科研项目列表
     */
    public function scientificIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['patentType'])) {
            $where['patentType'] = $param['patentType'];
        }
        if (isset($param['yearFrom']) && isset($param['yearTo'])) {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom'])) {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo'])) {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege'])) {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator'])) {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        if (isset($param['projectFund'])) {
            $where['projectFund'] = $param['projectFund'];
        }
        $list = model('collection')
            ->field("title,creator,projectNo,projectFund,level,publicationDate")
            ->where('resourceCode', '=', 'O')
            ->where($where)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('scientificIndex', ['list' => $list]);
    }

    /**
     * 科研项目导出
     */
    public function scientificExport()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['patentType']) && $param['patentType'] != '') {
            $where['patentType'] = $param['patentType'];
        }
        if (isset($param['yearFrom']) && $param['yearFrom'] != '' && isset($param['yearTo']) && $param['yearTo'] != '') {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom']) && $param['yearFrom'] != '') {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo']) && $param['yearTo'] != '') {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege']) && $param['secondaryCollege'] != '') {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator']) && $param['creator'] != '') {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        if (isset($param['projectFund']) && $param['projectFund'] != '') {
            $where['projectFund'] = $param['projectFund'];
        }
        $data = model('collection')
            ->field("projectNo,title,creator,projectFund,level,publicationDate")
            ->where('resourceCode', '=', 'O')
            ->where($where)
            ->select()
            ->toArray();
        array_unshift($data, ['编号', '项目名称', '项目主持人', '项目级别', '赞助基金', '公开时间']);
        insert_admin_log('导出了科研项目');
        $res = export_excel_ajax($data, date('YmdHis'));
        return $this->result($res, 1);
    }

    /**
     * 机构合作折线图
     */
    public function cooperationChartIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['resourceCode'])) {
            $where['resourceCode'] = $param['resourceCode'];
        }
        if (isset($param['yearFrom']) && isset($param['yearTo'])) {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom'])) {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo'])) {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege'])) {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator'])) {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        $areaList = db('resource_collection')->alias('rc')
            ->join('resource_cooperate_info rci', 'rci.resource_id = rc.id', 'right')
            ->field('rci.area, COUNT(rci.area) areaCount')
            ->where($where)
            ->group('rci.area');

        $cooperateList = model('collection')->field('year,COUNT(resourceCode) count,resourceCode')
            ->where($where)
            ->group('year,resourceCode')->order('year,resourceCode')->select()->toArray();
        $obj['areaList'] = $areaList;
        $obj['cooperateList'] = $cooperateList;
        return $this->result($obj, 1);
    }

    /**
     * 机构合作列表
     */
    public function cooperationIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['resourceCode'])) {
            $where['resourceCode'] = $param['resourceCode'];
        }
        if (isset($param['yearFrom']) && isset($param['yearTo'])) {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom'])) {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo'])) {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege'])) {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator'])) {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        $list = model('collection')
            ->field("projectNo,title,creator,journal,volume,url,create_time")
            ->where($where)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('cooperationIndex', ['list' => $list]);
    }

    /**
     * 机构合作导出
     */
    public function cooperationExport()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['resourceCode']) && $param['resourceCode'] != '') {
            $where['resourceCode'] = $param['resourceCode'];
        }
        if (isset($param['yearFrom']) && $param['yearFrom'] != '' && isset($param['yearTo']) && $param['yearTo'] != '') {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom']) && $param['yearFrom'] != '') {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo']) && $param['yearTo'] != '') {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege']) && $param['secondaryCollege'] != '') {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator']) && $param['creator'] != '') {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        $data = model('collection')
            ->field("projectNo,title,creator,journal,volume,url,create_time")
            ->where($where)
            ->select()
            ->toArray();
        array_unshift($data, ['编号', '标题', '作者', '期刊', '年卷期', '原文地址', '入库时间']);
        insert_admin_log('导出了机构合作');
        $res = export_excel_ajax($data, date('YmdHis'));
        return $this->result($res, 1);
    }

    /**
     * 专著统计折线图
     */
    public function treatiseChartIndex()
    {
        $years = model('collection')->field('year, count(year) as count')->where('resourceCode', '=', 'B')
            ->group('year')->order('year')->select()->toArray();
        $obj['years'] = $years;
        return $this->result($obj, 1);
    }

    /**
     * 专著统计列表
     */
    public function treatiseIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['yearFrom']) && isset($param['yearTo'])) {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom'])) {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo'])) {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege'])) {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator'])) {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        if (isset($param['level'])) {
            $where['level'] = $param['level'];
        }
        $list = model('collection')
            ->field("projectNo,title,creator,publisher,isbn,create_time")
            ->where('resourceCode', '=', 'B')
            ->where($where)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('treatiseIndex', ['list' => $list]);
    }

    /**
     * 专著统计导出
     */
    public function treatiseExport()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['patentType']) && $param['patentType'] != '') {
            $where['patentType'] = $param['patentType'];
        }
        if (isset($param['yearFrom']) && $param['yearFrom'] != '' && isset($param['yearTo']) && $param['yearTo'] != '') {
            $where['year'] = array('between', array($param['yearFrom'], $param['yearTo']));
        } else {
            if (isset($param['yearFrom']) && $param['yearFrom'] != '') {
                $where['year'] = array('egt', $param['yearFrom']);
            }
            if (isset($param['yearTo']) && $param['yearTo'] != '') {
                $where['year'] = array('elt', $param['yearTo']);
            }
        }
        if (isset($param['secondaryCollege']) && $param['secondaryCollege'] != '') {
            $where['secondaryCollege'] = $param['secondaryCollege'];
        }
        if (isset($param['creator']) && $param['creator'] != '') {
            $where['creator'] = ['like', "%" . $param['creator'] . "%"];
        }
        if (isset($param['level']) && $param['level'] != '') {
            $where['level'] = $param['level'];
        }
        $data = model('collection')
            ->field("projectNo,title,creator,publisher,isbn,create_time")
            ->where('resourceCode', '=', 'B')
            ->where($where)
            ->select()
            ->toArray();
        array_unshift($data, ['编号', '著作标题', '作者', '出版社', 'ISBN', '公开时间']);
        insert_admin_log('导出了专著统计');
        $res = export_excel_ajax($data, date('YmdHis'));
        return $this->result($res, 1);
    }


}
